package com.it.server.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.it.common.context.BaseContext;
import com.it.common.result.PageResult;
import com.it.common.result.Result;
import com.it.pojo.DTO.MerchantDTO;
import com.it.pojo.Entity.Merchant;
import com.it.pojo.Entity.Orders;
import com.it.pojo.VO.MerchantDetailVO;
import com.it.pojo.VO.MerchantListVO;
import com.it.server.mapper.DishMapper;
import com.it.server.mapper.MerchantMapper;
import com.it.server.mapper.OrderMapper;
import com.it.server.service.MerchantService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
@RequiredArgsConstructor
public class MerchantServiceImpl implements MerchantService {

    private final MerchantMapper merchantMapper;

    private  final DishMapper dishMapper;
    private  final OrderMapper orderMapper;


//    @Override
//    public List<MerchantVO> list() {
//        List<MerchantVO> list = merchantMapper.list();
//        return list;
//    }

    @Override
    public MerchantDetailVO detail(Integer id) {
        MerchantDetailVO merchantDetailVO  = merchantMapper.detail(id);
        merchantDetailVO.setGoods(dishMapper.list(id));
        merchantDetailVO.setCategories(merchantMapper.getCategories(merchantDetailVO.getId()));
        return merchantDetailVO;
    }

    @Override
    public PageResult<MerchantListVO> query(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Page<MerchantListVO> pages = merchantMapper.list();
        return new PageResult(pages.getTotal(),pages.getResult());
    }

    @Override
    public List<MerchantListVO> listCategory(Integer categoryId) {
        List<MerchantListVO> list = merchantMapper.listCategory(categoryId);
        return list;
    }

    @Override
    public Result login(MerchantDTO merchantDTO) {
        // 1. 查询商户信息
        Merchant merchant = BeanUtil.copyProperties(merchantDTO, Merchant.class);
        Merchant info = merchantMapper.login(merchant);
        // 2. 校验商户信息
        if (merchant == null){
            return Result.error("账号或密码错误");
        }
        return Result.success(merchant);
    }

    /**
     *  注册
     * @param merchant
     * @return
     */
    @Override
    public Result register(Merchant merchant) {
        merchant.setCreatedAt(DateTime.now().toLocalDateTime());
        merchant.setUpdatedAt(DateTime.now().toLocalDateTime());
        merchantMapper.add(merchant);
        return Result.success();
    }

    /**
     * 信息修改
     * @param merchant
     * @return
     */
    @Override
    public Result updata(Merchant merchant) {
        // TODO: 1.获取当前商户id
        Long merchantId = BaseContext.getCurrentMerchantId();
        // 2.更新商户信息
        merchantMapper.update(merchant);
        return null;
    }

    @Override
    public Result queryOrder(Integer page, Integer pageSize) {
        Long merchantId = BaseContext.getCurrentMerchantId();
        /*  TODO: 根据商户Id查询订单
        * */
        return null;
    }

    @Override
    public Result queryOrderById(Integer id) {

        Orders order = orderMapper.getById(id);
        if (order == null){
            return Result.error("订单不存在");
        }
        return Result.success(order);
    }
}
